package com.example.service.Imp;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.b_pojo.Musiccomment;
import com.example.common.b_pojo.Musicuser;
import com.example.common.dto.CUserdto;
import com.example.common.dto.CommentParam;
import com.example.common.dto.Commentdto;
import com.example.common.dto.Musiccommondto;
import com.example.common.pojo.musicarticle;
import com.example.common.pojo.musiccomment;
import com.example.common.pojo.musicuser;
import com.example.dao.MusicCommentMapper;
import com.example.dao.MusicUserMapper;
import com.example.dao.mapper.BMusiccommentMapper;
import com.example.service.MusicArticleservice;
import com.example.service.MusicCommentservice;
import com.example.service.MusicUserservice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class MusicCommentserviceImp extends ServiceImpl<MusicCommentMapper, musiccomment> implements MusicCommentservice {

    @Autowired
    private BMusiccommentMapper bMusiccommentMapper;

    @Autowired
    private MusicCommentMapper musicCommentMapper;

    @Autowired
    private MusicUserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MusicUserservice musicUserservice;

    @Autowired
    private MusicArticleservice musicArticleservice;

    /*
    * TODO 存在循环查询，十分影响性能
    * */

    /*
    * 参数为文章id
    * */
    @Override
    public List<Commentdto> commentlist(Long commentId) {
        LambdaQueryWrapper<musiccomment> wapper = new LambdaQueryWrapper<>();
        wapper.eq(musiccomment::getArticleId,commentId);
        wapper.eq(musiccomment::getLevel,"1");
        List<musiccomment> musiccommentlist = musicCommentMapper.selectList(wapper);
        List<Commentdto> copylistdto = this.copylistdto(musiccommentlist);
        return copylistdto;
    }

    @Override
    public boolean commentadd(CommentParam commentParam, String token) {
        String userinfo = stringRedisTemplate.opsForValue().get(token);
        musicuser musicuserinfo = JSON.parseObject(userinfo, musicuser.class);
        musiccomment musiccomment = new musiccomment();
        BeanUtils.copyProperties(commentParam,musiccomment);
        musiccomment.setAuthorId(musicuserinfo.getId());
        if (Integer.valueOf(musiccomment.getLevel()) == 2 && musiccomment.getParentId()==null){
            return false;
        }
        int insert = musicCommentMapper.insert(musiccomment);
        if (insert==1){
            return true;
        }
        return false;
    }

    @Override
    public List<Musiccommondto> selectAllComment(int pageNum, int pageSize, String content, Long id) {
        Page<Musiccomment> page = new Page<>(pageNum,pageSize);
        Page<Musiccomment> musiccommentPage=bMusiccommentMapper.selectallComment(page,content,id);
        List<Musiccomment> records = musiccommentPage.getRecords();


        List<Musiccommondto> musiccommondtos = records.stream().map(item -> {

            Musiccommondto musiccommondto = new Musiccommondto();
            BeanUtils.copyProperties(item,musiccommondto);

            if(musicUserservice.getuserById(item.getAuthorId()) != null){
                musiccommondto.setAuthorName(musicUserservice.getuserById(item.getAuthorId()).getName());
            }
            if(musicArticleservice.selectArticleById(item.getArticleId()) != null){
                musiccommondto.setArticleName(musicArticleservice.selectArticleById(item.getArticleId()).getTitle());
            }
            return musiccommondto;
        }).collect(Collectors.toList());
        return musiccommondtos;
    }

    @Override
    public boolean deleteCommentsByIds(List<Long> ids) {
        // 实现删除逻辑
        for (Long id : ids) {
            // 调用删除单条评论的方法
            int result=bMusiccommentMapper.deleteAllbyId(id);
            if (result != 1) {
                // 如果某条评论删除失败，返回false
                return false;
            }
        }
        // 所有评论删除成功
        return true;
    }

    public List<Commentdto> copylistdto(List<musiccomment> clist){
        List<Commentdto> commentdtolist = new ArrayList<>();
        for (musiccomment m:clist
             ) {
            Commentdto copydto = this.copydto(m);
            commentdtolist.add(copydto);
        }
        return commentdtolist;
    }

    public Commentdto copydto(musiccomment m){
        Commentdto commentdto = new Commentdto();
        BeanUtils.copyProperties(m,commentdto);
        commentdto.setCUserdto(this.CUser(m.getAuthorId()));
        List<Commentdto> findparentlist = this.findparentlist(m.getId());
        commentdto.setChildrend(findparentlist);
        return commentdto;
    }

    public List<Commentdto> findparentlist(Long id){
        System.out.println(id);
        LambdaQueryWrapper<musiccomment> wapper = new LambdaQueryWrapper<>();
        wapper.eq(musiccomment::getParentId,id);
        wapper.eq(musiccomment::getLevel,"2");
        List<musiccomment> musiccomments = musicCommentMapper.selectList(wapper);
        return copylistdto(musiccomments);
    }

    public CUserdto CUser(Long authorId){
        musicuser musicuser = userMapper.selectById(authorId);
        CUserdto cUserdto = new CUserdto();
        try {
            BeanUtils.copyProperties(musicuser,cUserdto);
        }catch (Exception e){
            log.info("数据库用户数据存在异常消失，从而导致查询异常，报错！");
            e.printStackTrace();
        }
        return cUserdto;
    }

}
